package com.kws.merchant.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.kws.common.config.constant.MinioCommonBucket;
import com.kws.merchant.domain.Item;
import com.kws.merchant.domain.ItemMaterial;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kws.merchant.domain.po.ItemMaterialPo;
import com.kws.merchant.mapper.ItemMaterialMapper;
import com.kws.merchant.service.ItemMaterialService;
import com.kws.merchant.service.ItemService;
import com.kws.merchant.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 报价-项目资料管理(ItemMaterial)表服务实现类
 *
 * @author yxy
 * @since 2024-10-24 14:46:59
 */
@Service("itemMaterialService")
public class ItemMaterialServiceImpl extends ServiceImpl<ItemMaterialMapper, ItemMaterial> implements ItemMaterialService {

    @Resource
    private ItemMaterialMapper itemMaterialMapper;
    @Autowired
    private ItemService itemService;
    @Value("${minio.ENDPOINT}")
    private String minioUrl;
    /**
     * 分页查询列表
     */
    @Override
    public Page<ItemMaterial> pageList(ItemMaterialPo itemMaterialPo) {
        LambdaQueryWrapper<ItemMaterial> queryWrapper = Wrappers.<ItemMaterial>lambdaQuery();
        String fileName = itemMaterialPo.getFileName();//文件名称
        String itemName = itemMaterialPo.getItemName();//项目名称
        String startTime = itemMaterialPo.getStartTime();//开始时间
        String endTime = itemMaterialPo.getEndTime();//结束时间
        if (StringUtils.isNotEmpty(fileName)){
            queryWrapper.eq(ItemMaterial::getFileName,fileName);
        }
        /*if (StringUtils.isNotEmpty(itemName)){
            queryWrapper.eq(ItemMaterial::geti,itemName);
        }*/
        if (StringUtils.isNotEmpty(startTime)&&StringUtils.isNotEmpty(endTime)){
            queryWrapper.between(ItemMaterial::getCreateTime,startTime,endTime);
        }
        Page<ItemMaterial> page = this.page(new Page<>(itemMaterialPo.getPageSize(), itemMaterialPo.getTotalPage()), queryWrapper);
        List<ItemMaterial> records = page.getRecords();
        records.forEach(x->{
            Item item = itemService.getById(x.getItemId());
            if (ObjectUtils.isEmpty(item)){
                return;
            }
            x.setItemName(item.getItemName());
        });
        if (StringUtils.isNotEmpty(itemName)){
            records= records.stream().filter(x->x.getItemName().equals(itemName)).collect(Collectors.toList());
        }
        page.setRecords(records);
        return page;
    }

    @Override
    public Object detailById(Long id) {
        ItemMaterial itemMaterial = this.getById(id);
        itemMaterial.setFilePath(FileUtil.fixFileName()+itemMaterial.getFilePath());
        return itemMaterial;
    }


}
